package util;

import java.security.MessageDigest;
import java.util.Enumeration;
import java.util.Formatter;
import java.util.Hashtable;

/**
 * The Tool {@code class} containing all common method.
 * 
 * @author Peter NGO, Guillaume POUSSEL and Mathieu RUL
 * 
 * @version 1.0
 */
public class Tool {

	/**
	 * Give the sha1 hash of a string
	 * 
	 * @param string
	 *            the string to hash.
	 * 
	 * @return the sha1 hash.
	 */
	public static String Sha1(String string) {
		String result = "";
		try {
			Formatter formatter = new Formatter();
			for (byte b : MessageDigest.getInstance("SHA-1").digest(
					string.getBytes()))
				formatter.format("%02x", b);
			result = formatter.toString();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return result;
	}

	/**
	 * Give the maximum ID of an Hashtable
	 * 
	 * @param hashTable
	 *            <Integer, Object> the Hashtable.
	 * 
	 * @return the maximum ID.
	 */
	public static Integer getMaxID(Hashtable<Integer, Object> hashTable) {
		Integer result = new Integer(0);
		Enumeration<Integer> e = hashTable.keys();
		while (e.hasMoreElements()) {
			Integer key = e.nextElement();
			result = (key > result) ? key : result;
		}
		return result;
	}

	/**
	 * Convert a string to a correct variable name.
	 * 
	 * @param string
	 *            the string to convert.
	 *            
	 * @return the converted string.
	 */
	public static String toVarName(String string) {
		return string.toLowerCase();
	}

	/**
	 * Convert a string to a correct class name.
	 * 
	 * @param string
	 *            the string to convert.
	 *            
	 * @return the converted string.
	 */
	public static String toClassName(String string) {
		return String.valueOf(string.charAt(0)).toUpperCase()
				+ string.substring(1, string.length());
	}

	/**
	 * Convert a string to plural.
	 * 
	 * @param string
	 *            the string to convert.
	 *            
	 * @return the converted string.
	 */
	public static String plural(String string) {
		return string + "s";
	}

	/**
	 * Convert a string to singular.
	 * 
	 * @param string
	 *            the string to convert.
	 *            
	 * @return the converted string.
	 */
	public static String singular(String string) {
		return string.substring(0, string.length() - 1);
	}
};